﻿Imports System
Imports System.Globalization
Imports System.Text.RegularExpressions
Imports System.Xml.Serialization

Namespace System.Workflow.ComponentModel.Compiler
    <XmlType("authorizedType")> _
    Public NotInheritable Class AuthorizedType
        ' Methods
        Private Shared Function MakeRegex(ByVal inputString As String) As String
            Return inputString.Replace("\", "\\").Replace("[", "\[").Replace("^", "\^").Replace("$", "\$").Replace("|", "\|").Replace("+", "\+").Replace("(", "\(").Replace(")", "\)").Replace(".", "\x2E").Replace("*", "[\w\x60\x2E]{0,}").Replace("?", "\w{1,1}").Replace(" ", "\s{0,}")
        End Function


        ' Properties
        <XmlAttribute> _
        Public Property [Assembly] As String
            Get
                Return Me.assemblyName
            End Get
            Set(ByVal value As String)
                Me.assemblyName = value
            End Set
        End Property

        <XmlAttribute> _
        Public Property Authorized As String
            Get
                Return Me.isAuthorized.ToString
            End Get
            Set(ByVal value As String)
                Me.isAuthorized = Boolean.Parse(value)
            End Set
        End Property

        <XmlAttribute> _
        Public Property [Namespace] As String
            Get
                Return Me.namespaceName
            End Get
            Set(ByVal value As String)
                Me.namespaceName = value
            End Set
        End Property

        <XmlIgnore> _
        Public ReadOnly Property RegularExpression As Regex
            Get
                If (Me.regex Is Nothing) Then
                    Me.regex = New Regex(AuthorizedType.MakeRegex(String.Format(CultureInfo.InvariantCulture, "{0}.{1}, {2}", New Object() { Me.namespaceName, Me.typeName, Me.assemblyName })), RegexOptions.Compiled)
                    Return Me.regex
                End If
                Return Me.regex
            End Get
        End Property

        <XmlAttribute> _
        Public Property TypeName As String
            Get
                Return Me.typeName
            End Get
            Set(ByVal value As String)
                Me.typeName = value
            End Set
        End Property


        ' Fields
        Private assemblyName As String
        Private isAuthorized As Boolean
        Private namespaceName As String
        Private regex As Regex
        Private typeName As String
    End Class
End Namespace

